package com.chen.core.util;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.pojo.PageParam;
import com.chen.pojo.SortField;

import java.util.Collection;
import java.util.stream.Collectors;

public class MyBatisPlusUtil {
    public static<T> Page<T> buildPage(PageParam pageParam){
        return buildPage(pageParam,null);
    }

    public static <T> Page<T> buildPage(PageParam pageParam, Collection<SortField> sortFields){
        //设置对应的page
        Page<T> page = new Page<>(pageParam.getPageNo(),pageParam.getPageSize());
        //设置对应的排序方法
        if(CollectionUtil.isNotEmpty(sortFields)){
            page.addOrder(sortFields.stream().map(
                    sortField -> {
                        return SortField.ORDER_ASC.equals(sortField.getOrder())?
                                new OrderItem(sortField.getField(),true):
                                new OrderItem(sortField.getField(),false);
                    }
            ).collect(Collectors.toList()));
        }
        //返回page
        return page;
    }
}
